package com.xiaoxian.org.service.impl;

import com.xiaoxian.basic.commoneumn.ErrorCode;
import com.xiaoxian.basic.constant.Constant;
import com.xiaoxian.basic.domain.vo.ShopStatisticsVo;
import com.xiaoxian.basic.exception.CustomException;
import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.BaiduAuditUtils;
import com.xiaoxian.org.domain.Employee;
import com.xiaoxian.user.domain.LoginInfo;
import com.xiaoxian.org.domain.Shop;
import com.xiaoxian.org.domain.ShopAuditLog;
import com.xiaoxian.org.mapper.EmployeeMapper;
import com.xiaoxian.user.mapper.LogininfoMapper;
import com.xiaoxian.org.mapper.ShopAuditLogMapper;
import com.xiaoxian.org.mapper.ShopMapper;
import com.xiaoxian.org.service.IShopService;
import com.xiaoxian.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxian
 * @since 2022-12-17
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    @Transactional
    public void setShop(Shop shop) {

        //参数校验
        //校验两次密码是否正确
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new CustomException(ErrorCode.SHOP_PASSWORD_DEF.getCode());
        }
        //判断商店是否存在
        Shop shopTeml = shopMapper.findByName(shop.getName());
        if(shopTeml!=null){
            throw new CustomException(ErrorCode.SHOP_EXIST.getCode());
        }
        //百度文本审核
        Boolean text=false;
        Boolean imgCensor=false;
        text=BaiduAuditUtils.TextCensor(shop.getName());
        //百度图片审核
        if(shop.getLogo()!=null){
            imgCensor = BaiduAuditUtils.ImgCensor(Constant.HTTP_URL_PRE+shop.getLogo());
        }
        if(!text || !imgCensor){
            throw new CustomException(ErrorCode.AI_CODE.getCode());
        }
        //初始化店铺状态，1 正常，0审核中 -1驳回
        shop.setState(0);
        //初始话时间
        shop.setRegisterTime(new Date());
        //插入店铺
        shopMapper.add(shop);
        //判断店长是否存在
        Employee employee = employeeMapper.findByUsername(shop.getAdmin().getUsername());
        if(employee !=null && employee.getShop_id()!=null){
            throw new CustomException(ErrorCode.SHOP_EXIST_EMP.getCode());
        }
        //判断店长是否存在
        if(employee!=null){
            //绑定店铺id
            employee.setShop_id(shop.getId());
            //给店铺绑定管理者得id
            shop.setAdminId(employee.getId());
        }else {
            //给店长绑定店铺id
            shop.getAdmin().setShop_id(shop.getId());
            //获取
            shop.getAdmin().setState(1);
            //员工表插入店长信息
            employeeMapper.add(shop.getAdmin());

            //给店铺绑定id
            shop.setAdminId(shop.getAdmin().getId());
            //查询插入得员工
            Employee shopAdmin = employeeMapper.findOne(shop.getAdmin().getId());

            //给登录表插入员工信息
            LoginInfo logininfo = new LoginInfo();
            logininfo.setType(0);
            BeanUtils.copyProperties(shopAdmin,logininfo);
            logininfoMapper.add(logininfo);
            //查询插入logininfo得id
            shopAdmin.setLogininfo_id(logininfo.getId());
            employeeMapper.update(shopAdmin);


        }
        //更新店铺
        shopMapper.update(shop);

    }

    /**
     * 商户审核通过
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //查询shop信息
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());
        //修改商户状态
        shop.setState(2);
        //插入数据库
        shopMapper.update(shop);
        //设置日志状态
        //修改日志状态
        shopAuditLog.setState(1);
        //更新操作人
        shopAuditLog.setAuditId(325L);
        //更新操作时间
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.add(shopAuditLog);
//        //发送邮件
//        SimpleMailMessage simple = new SimpleMailMessage();
//        //设置发送人
//        simple.setFrom("411246733@qq.com");
//        //标题
//        simple.setSubject("我爱唱跳rap篮球");
//        //设置收件人
//        simple.setTo("411246733@qq.com");
//        //设置发送内容
//        simple.setText("http://localhost:8080/shop/active/"+shop.getId());
//        javaMailSender.send(simple);

    }
    /**
     * 商户审核驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //修改商铺状态
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);

        //修改日志状态类容
        shopAuditLog.setState(0);
        //修改日志审核人
        shopAuditLog.setAuditId(325L);
        //更新日期
        shopAuditLog.setAuditTime(new Date());
        //插入日志数据
        shopAuditLogMapper.add(shopAuditLog);

        //发送邮件
//        SimpleMailMessage simple = new SimpleMailMessage();
//        //设置发送人
//        simple.setFrom("411246733@qq.com");
//        //标题
//        simple.setSubject("我爱唱跳rap篮球");
//        //设置收件人
//        simple.setTo("411246733@qq.com");
//        //设置发送内容
//        simple.setText("http://localhost:8081/#/shop2?id="+shop.getId());
//        javaMailSender.send(simple);
    }

    /**
     * 前端统计图
     * @return
     */
    @Override
    public AjaxResult getState() {
        List<ShopStatisticsVo> state = shopMapper.findState();
        List<Integer> x=new ArrayList<>();
        List<Integer> xValue=new ArrayList<>();
        for (ShopStatisticsVo shopStatisticsVo : state) {
            x.add(shopStatisticsVo.getState());
            xValue.add(shopStatisticsVo.getCount());
        }
        HashMap<String, List<Integer>> map = new HashMap<>();
        map.put("x",x);
        map.put("xValue",xValue);
        return AjaxResult.my().setPath(map);
    }

}
